//
//  SCUserTool.m
//  Scalper
//
//  Created by 王云 on 2017/9/23.
//  Copyright © 2017年 WY. All rights reserved.
//

#import "SCUserTool.h"
#import "SCUser.h"

#define kUserDataPath           [kDirDoc stringByAppendingPathComponent:@"userdata.archiver"]


@implementation SCUserTool


//读取
+ (SCUser *)loadInfo{
    SCUser *userInfo = [SCUser sharedSCUser];
    if(userInfo.isLogin){
        return userInfo;
    }else{
        return (SCUser *)[NSKeyedUnarchiver unarchiveObjectWithFile:kUserDataPath];
    }
    
}
//登陆 退出
+ (void)logIn:(SCUser *)userInfo{
    
    [NSKeyedArchiver archiveRootObject:userInfo toFile:kUserDataPath];
}
+ (void)logOut{
    SCUser *userInfo = [SCUser sharedSCUser];
    if (userInfo.isLogin) {
        userInfo.isLogin = NO;
        userInfo.token = nil;
        userInfo = nil;
        NSFileManager *mgr = [NSFileManager defaultManager];
        [mgr removeItemAtPath:kUserDataPath error:nil];
        [[NSNotificationCenter defaultCenter] postNotificationName:kUserDataLogOut object:nil userInfo:nil];

    }
}
//修改
+ (void)saveData:(SCUser *)userInfo{
    
    [NSKeyedArchiver archiveRootObject:userInfo toFile:kUserDataPath];
//    [[NSNotificationCenter defaultCenter] postNotificationName:CBCHANGEDATA object:userInfo userInfo:@{@"info":userInfo}];
}
//不发送通知的修改
+ (void)changeData:(SCUser *)userInfo{
    
    [NSKeyedArchiver archiveRootObject:userInfo toFile:kUserDataPath];
}
//用户登录
+ (SCUser *)loadUserData:(NSDictionary *)userData{
    SCUser *userInfo = [[SCUser alloc] init];
    userInfo.token   = [userData hw_safeObjectForKey:@"token"];
    userInfo.isLogin        = YES;

    NSDictionary *member_info = [userData hw_safeObjectForKey:@"member_info"];
    userInfo.member_avatar = [member_info hw_safeObjectForKey:@"member_avatar"];
    
    userInfo.member_id = [NSString stringWithFormat:@"%@",[member_info hw_safeObjectForKey:@"member_id"]];
    
    userInfo.member_mobile = [NSString stringWithFormat:@"%@",[member_info hw_safeObjectForKey:@"member_mobile"]];
    
    userInfo.member_name = [NSString stringWithFormat:@"%@",[member_info hw_safeObjectForKey:@"member_name"]];
    
    userInfo.member_sex = [NSString stringWithFormat:@"%@",[member_info hw_safeObjectForKey:@"member_sex"]];
    
    userInfo.member_type = [NSString stringWithFormat:@"%@",[member_info hw_safeObjectForKey:@"member_type"]];
    
    if ([member_info hw_safeObjectForKey:@"member_email_bind"]) {
        userInfo.member_email_bind = [NSString stringWithFormat:@"%@",[member_info hw_safeObjectForKey:@"member_email_bind"]];
    }
    if ([member_info hw_safeObjectForKey:@"member_state"]) {
        userInfo.member_state = [NSString stringWithFormat:@"%@",[member_info hw_safeObjectForKey:@"member_state"]];
    }
    if ([member_info hw_safeObjectForKey:@"joinin_state"]) {
        userInfo.joinin_state = [NSString stringWithFormat:@"%@",[member_info hw_safeObjectForKey:@"joinin_state"]];
    }else{
        userInfo.joinin_state = @"0";
    }
    
    if ([member_info hw_safeObjectForKey:@"write_off_member_type"]) {
        userInfo.write_off_member_type = [[member_info hw_safeObjectForKey:@"write_off_member_type"] integerValue];
    }else{
        userInfo.write_off_member_type = 0;
    }
    
    userInfo.member_mobile_bind = [[member_info hw_safeObjectForKey:@"member_mobile_bind"] integerValue];
    
    return userInfo;
}

@end
